import { Bone } from "./Bone";
import { Rectangle } from "../geom/Rectangle";
import { Slot } from "./Slot";
import { Skin } from "./Skin";
import rootStore from "../../stores";

enum ArmatureType {
    Armature = 0,
    MovieClip = 1,
    Stage = 2,
    ImageSequences = 3
}

class Armature {
    type: ArmatureType | string = ArmatureType[ArmatureType.Armature].toLowerCase();
    frameRate: number = 0;
    name: string = "";
    aabb: Rectangle = new Rectangle();
    bone: Bone[] = [];
    slot: Slot[] = [];
    skin: Skin[] = [];

    public addBone(item: Bone, level: number) {
        this.bone.push(item);
        console.log("add bone", item, level);
        rootStore.sceneStore.addBone(item.parent, item, level);
    }

    public selectBone(boneName: string) {
        rootStore.sceneStore.unFoldBone(boneName);
        rootStore.sceneStore.setSelected(boneName);
    }

    public addSlot(slot: Slot) {
        this.slot.push(slot);
        rootStore.sceneStore.addSlot(slot.parent, slot.name);
    }

    public addSkin(skin: Skin) {
        this.skin.push(skin);
        rootStore.sceneStore.addSkin(skin.name);
    }

    public genereateSlotName(baseName:string)
    {
        let name = baseName;
        while (this.checkSlotNameExist(name)) {
            name = name + "1";
        }

        return name;
    }

    public checkSlotNameExist(slotName: string) {
        for (const item of this.slot) {
            if (item.name === slotName) {
                return true;
            }
        }
        return false;
    }
}

export { Armature };